package com.roobo.demo;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 二维码页面
 */


public class QRCodeActivity extends AppCompatActivity {

    public static final String TAG = QRCodeActivity.class.getSimpleName();
    public static final String MESSAGE = "message";
    public static final String QR_CODE_URL = "qrcodeUrl";
    public static final int CONNECT_TIME_OUT = 3000;

    private TextView mMessageTv;
    private ImageView mQRCodeIv;

    private Bitmap mBitmap;

    private MyHandler mMyHandler;
    private ProgressDialog mProgressDialog;

    /**
     * 静态内部类
     */
    static class MyHandler extends Handler {
        private WeakReference<QRCodeActivity> mWeakReference;

        public MyHandler(QRCodeActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mWeakReference.get() == null) {
                return;
            }
            mWeakReference.get().setQRCodeView();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code);
        setTitle(getResources().getString(R.string.qrcode_activity_title));
        initViews();
        mMyHandler = new MyHandler(this);
    }

    private void initViews() {
        String message = getIntent().getStringExtra(MESSAGE);
        mMessageTv = findViewById(R.id.message_tv);
        mMessageTv.setText(message);
        mQRCodeIv = findViewById(R.id.qr_code_iv);
        String qrCodeUrl = null;
        try {
            JSONObject object = new JSONObject(message);
            qrCodeUrl = object.getString(QR_CODE_URL);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "initViews e.getLocalizedMessage:" + e.getLocalizedMessage());
        }
        if (!TextUtils.isEmpty(qrCodeUrl)) {
            getUrlImage(qrCodeUrl);
        } else {
            Log.d(TAG, "initViews qrCode为空");
        }
    }

    /***
     * 加载图片
     * @param qrCodeUrl
     */
    public void getUrlImage(final String qrCodeUrl) {
        showProgressDialog();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(qrCodeUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(CONNECT_TIME_OUT);//设置超时
                    conn.setReadTimeout(CONNECT_TIME_OUT);
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();//获得图片的数据流
                    mBitmap = BitmapFactory.decodeStream(is);
                    is.close();
                    mMyHandler.sendEmptyMessage(0);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, "getUrlImage e.getLocalizedMessage:" + e.getLocalizedMessage());
                }
            }
        }.start();
    }

    public void setQRCodeView() {
        dismissProgressDialog();
        if (mBitmap != null) {
            mQRCodeIv.setImageBitmap(mBitmap);
        }
    }

    private void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setTitle(getResources().getString(R.string.dialog_title));
            mProgressDialog.setMessage(getResources().getString(R.string.dialog_message));
        }
        if (!mProgressDialog.isShowing())
            mProgressDialog.show();
    }

    private void dismissProgressDialog() {
        if (!this.isFinishing() && mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissProgressDialog();
    }
}
